//
//  PictureViewController.m
//  sinawb
//
//  Created by ibokan on 14-11-13.
//  Copyright (c) 2014年 ibokan. All rights reserved.
//

#import "PictureViewController.h"
#import "AFNetworking.h"

@interface PictureViewController ()
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@property (strong, nonatomic) IBOutlet UITextView *textViewField;
- (IBAction)publishPressed:(id)sender;

@end

@implementation PictureViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIImage *img = [UIImage imageNamed:@"a.jpg"];
    self.imageView.image = img;
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)publishPressed:(id)sender {
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSString *token = (NSString *)[[NSUserDefaults standardUserDefaults]objectForKey:@"accessToken"];
    
//    UIImage *img = [UIImage imageNamed:@"a.jpg"];
//    NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"a" ofType:@"jpg"]];
//    NSData *data = UIImageJPEGRepresentation(img, 0.5);
    NSURL *imgUrl = [[NSURL alloc]initFileURLWithPath:[[NSBundle mainBundle]pathForResource:@"a" ofType:@"jpg"]];
    
    NSString *url = [self.textViewField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSDictionary *dic = @{@"access_token": token,@"status":url};
    
    [manager POST:@"https://upload.api.weibo.com/2/statuses/upload.json" parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileURL:imgUrl name:@"hurl" error:nil];
//        NSLog(@"%@",formData);
        NSLog(@"999999999");
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"111111111");
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
    
    
    
//    [manager POST:@"https://upload.api.weibo.com/2/statuses/upload.json" parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        NSDictionary *dic = responseObject;
//        NSLog(@"%@",dic);
//        [[NSNotificationCenter defaultCenter]postNotificationName:@"PUBLISH" object:nil userInfo:nil];
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        NSLog(@"%@",error);
//    }];
}




























@end
